﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.IO;

namespace dbgptc
{
    public class TunnelSettings
    {
        public string tunnelAddress { get; set; }
        public int tunnelPort { get; set; }
        public string dbgpSessionAddress { get; set; }
        public int dbgpSessionPort { get; set; }
        public string ideKey { get; set; }

        public TunnelSettings()
        {
            tunnelAddress = "localhost";
            tunnelPort = 9100;
            dbgpSessionAddress = "localhost";
            dbgpSessionPort = 9000;
            ideKey = "ECLIPSE_DBGP";
        }

        public static TunnelSettings load(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(TunnelSettings));
            if (!File.Exists(path)) return new TunnelSettings();

            using (FileStream fs = File.OpenRead(path)) {
                try {
                    return (TunnelSettings)serializer.Deserialize(fs);
                } catch {
                    return new TunnelSettings();
                }
            }
        }

        public bool save(string path)
        {
            try {
                using (FileStream fs = File.Create(path)) {
                    XmlSerializer serializer = new XmlSerializer(typeof(TunnelSettings));
                    serializer.Serialize(fs, this);
                    return true;
                }
            } catch {
                return false;
            }
        }
    }
}
